// vuex的状态机,根据功能模块划分为设备管理，设备点检，设备维修，设备保养等
import {
  fetchList,
  fetchFaultInfo,
  saveTroubleInfo,
  saveRepairedInfo,
  fetchRepairedInfo,
  saveInfo,
  saveCategoryInfo,
  saveDeviceInfo,
  fetchDetailInfo,
  exportFile,
  downloadTemplete,
  deleteDeviceInfo,
  sortDepartmentInfo
} from "@/api/system-devicecategory/index";
import { reduceNullContent, deepClone } from "@/utils/index";
const state = {
  listSearchParams: {},
  paginationInfo: {},
  records: [], //表格数据
  pages: 1,
  total: 20,
  mesg: "",
  /**
   * 编辑相关
   */
  detailInfo: {}, //详情模板
  totalForm: {},
  //故障配置对象
  fault: {},
  //维修配置
  repaired: ""
};
const formModel = {};
const mutations = {
  SET_LISTSERACH_PARAMS: (state, args) => {
    state.listSearchParams = args;
  },
  SET_LISTDATA: (state, args) => {
    state.records = args.records;
    state.pages = args.pages;
    state.current = args.current;
    state.total = args.total;
  },
  SET_BROAD_INFO: (state, args) => {
    state.totalForm[1].formComponent = args;
    state.totalForm[1].formModel = formModel;
  },
  SET_TOTAL_FORM: (state, args) => {
    state.totalForm = args;
  },
  SET_DETAIL_INFO: (state, args) => {
    state.detailInfo = args;
  },
  SET_PAGINATION: (state, args) => {
    state.paginationInfo = args;
  },
  SET_FAULT: (state, args) => {
    console.log(args, "+++args+++");
    state.fault = args;
  },
  SET_REPAIRED: (state, args) => {
    state.repaired = args;
  }
};

const actions = {
  // 设备台账查询参数
  setListSearchParams({ commit, state }, args) {
    const data = Object.assign({}, state.listSearchParams, args);
    commit("SET_LISTSERACH_PARAMS", data);
  },
  //列表
  requestDeviceList({ commit, state }, args) {
    return fetchList(reduceNullContent(args)).then(response => {
      console.log("设备台账", response);
      commit("SET_LISTDATA", response.data);
    });
  },
  setTotalForm({ commit, state }, args) {
    console.log(args, "total");
    commit("SET_TOTAL_FORM", args);
  },
  //保存设备分类
  saveCategoryInfo({ commit, state }, args) {
    return saveCategoryInfo(args);
  },
  //保存设备台账
  saveDeviceInfo({ commit, state }, args) {
    return saveDeviceInfo(args);
  },
  //保存账号
  saveInfo({ commit, state }, args) {
    return saveInfo(args);
  },
  //保存故障配置
  saveTroubleInfo({ commit, state }, args) {
    return saveTroubleInfo(state.fault);
  },
  //保存维修上限
  saveRepairedInfo({ commit, state }, args) {
    return saveRepairedInfo({ dicValue: args });
  },
  //部门排序
  sortDepartmentInfo({ commit, state }, args) {
    return sortDepartmentInfo(args);
  },
  //查询故障类型
  requestFault({ commit, state }, args) {
    return fetchFaultInfo(args)
      .then(res => {
        console.log(res.data, "+++res.data++");
        commit("SET_FAULT", res.data);
      })
      .catch(err => {});
  },
  //查询维修配置
  requestRepaired({ commit, state }, args) {
    return fetchRepairedInfo(args).then(res => {
      commit("SET_REPAIRED", res.data);
    });
    // .then(res => {
    //   commit("SET_REPAIRED", res.data);
    //   return;
    // })
    // .catch(err => {});
  },

  //删除设备台账
  deleteDeviceInfo({ commit, state }, args) {
    return deleteDeviceInfo(args);
  }
};

export default {
  namespaced: true,
  state,
  mutations,
  actions
};
